/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alphawavesgames.engine.utilities;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Andrew Dyer
 */
public class MessageList {

    private static List<GameMessage> messages;
    private static List<GameMessage> markedMessages;

    /**
     * Adds a String message to the list accompanied with a timestamp
     * (nano-time).
     *
     * @param message The string message to add.
     */
    public static void addGameMessage(String message) {
        GameMessage gameMessage = new GameMessage(message, System.currentTimeMillis());

        if (messages == null) messages = new ArrayList<GameMessage>();
        messages.add(gameMessage);
    }

    /**
     * Gets the list of text messages.
     *
     * @return The <code>List</code> of <code>String</code>s. Returns
     * <code>null</code> if the list has yet to be initiated by adding a
     * message or if it is empty.
     */
    public static List<String> getGameMessages() {
        if (messages == null) return null;
        if (messages.isEmpty()) return null;

        List<String> textList = new ArrayList<String>();
        for (GameMessage message : messages) {
            textList.add(message.getMessage());
        }

        return textList;
    }

    /**
     *
     * @param nanoseconds
     */
    public static void sweepExpiredMessages(long milliseconds) {
        if(messages == null) return;

        if(markedMessages == null) {
            markedMessages = new ArrayList<GameMessage>();
        }

        for (GameMessage message : messages) {
            if ((System.currentTimeMillis() - milliseconds) > message.getTimeAdded()) {
                markedMessages.add(message);
            }
        }

        if(!markedMessages.isEmpty()) {
            for (GameMessage deadMessage : markedMessages) {
                messages.remove(deadMessage);
            }
        }

        markedMessages.clear();
    }
}
